#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> result;
        map<string,int> M;
        int index = 1;
        for(int i =0;i<strs.size();++i){
            string tempstr = strs[i];
            sort(tempstr.begin(),tempstr.end());
            if(M[tempstr]==0){
                M[tempstr] = index;
                vector<string> TT;
                TT.push_back(strs[i]);
                result.push_back(TT);
                index++;
            }
            else{
                result[M[tempstr]-1].push_back(strs[i]);
            }
        }
        return result;
    }
};

int main(){
    vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    Solution S;
    vector<vector<string>> result = S.groupAnagrams(strs);
    return 0;
}